/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package other;

import B3D_Elements.B3D_Element;
import B3D_Elements.B3D_Filters.B3D_BasicSSAO;
import B3D_Elements.B3D_Filters.B3D_Bloom;
import B3D_Elements.B3D_Filters.B3D_Cartoon;
import B3D_Elements.B3D_Filters.B3D_ColorScale;
import B3D_Elements.B3D_Filters.B3D_Crosshatch;
import B3D_Elements.B3D_Filters.B3D_DepthOfField;
import B3D_Elements.B3D_Filters.B3D_Filter;
import B3D_Elements.B3D_Filters.B3D_Fog;
import B3D_Elements.B3D_Filters.B3D_FrostedGlass;
import B3D_Elements.B3D_Filters.B3D_LightScattering;
import B3D_Elements.B3D_Filters.B3D_OldFilm;
import B3D_Elements.B3D_Filters.B3D_Posterization;
import B3D_Elements.B3D_Filters.B3D_SSAO;
import B3D_Elements.B3D_Filters.B3D_Shadows.B3D_DLShadow;
import B3D_Elements.B3D_Filters.B3D_Shadows.B3D_PLShadow;
import B3D_Elements.B3D_Filters.B3D_Shadows.B3D_SLShadow;
import B3D_Elements.B3D_Filters.B3D_Water;
import B3D_Elements.B3D_Lights.B3D_AmbientLight;
import B3D_Elements.B3D_Lights.B3D_DirectionalLight;
import B3D_Elements.B3D_Lights.B3D_Light;
import B3D_Elements.B3D_Lights.B3D_PointLight;
import B3D_Elements.B3D_Lights.B3D_SpotLight;
import B3D_Elements.B3D_Physics.B3D_CShapes.B3D_BoxShape;
import B3D_Elements.B3D_Physics.B3D_CShapes.B3D_CShape;
import B3D_Elements.B3D_Physics.B3D_CShapes.B3D_CapsuleShape;
import B3D_Elements.B3D_Physics.B3D_CShapes.B3D_ConeShape;
import B3D_Elements.B3D_Physics.B3D_CShapes.B3D_CylinderShape;
import B3D_Elements.B3D_Physics.B3D_CShapes.B3D_DynamicMeshShape;
import B3D_Elements.B3D_Physics.B3D_CShapes.B3D_StaticMeshShape;
import B3D_Elements.B3D_Physics.B3D_Physics;
import B3D_Elements.B3D_Spatials.B3D_Geometrys.B3D_Box;
import B3D_Elements.B3D_Spatials.B3D_Geometrys.B3D_Cylinder;
import B3D_Elements.B3D_Spatials.B3D_Geometrys.B3D_ParticleEffect;
import B3D_Elements.B3D_Spatials.B3D_Geometrys.B3D_MultipleTextureSkyBox;
import B3D_Elements.B3D_Spatials.B3D_Geometrys.B3D_SingleTextureSkyBox;
import B3D_Elements.B3D_Spatials.B3D_Geometrys.B3D_Sphere;
import B3D_Elements.B3D_Spatials.B3D_Geometrys.B3D_Torus;
import B3D_Elements.B3D_Spatials.B3D_Model;
import B3D_Elements.B3D_Spatials.B3D_Node;
import B3D_Elements.B3D_Spatials.B3D_Spatial;
import B3D_Elements.Others.B3D_Material;
import B3D_Elements.Others.B3D_MaterialPropertyList;
import B3D_Elements.Others.B3D_MotionEvent;
import B3D_Elements.Others.B3D_MotionPath;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.ConeCollisionShape;
import com.jme3.bullet.collision.shapes.CylinderCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.cinematic.events.MotionEvent;
import com.jme3.effect.ParticleEmitter;
import com.jme3.effect.ParticleMesh;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.light.Light;
import com.jme3.light.PointLight;
import com.jme3.light.SpotLight;
import com.jme3.material.MatParam;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.post.Filter;
import com.jme3.post.filters.BloomFilter;
import com.jme3.post.filters.CartoonEdgeFilter;
import com.jme3.post.filters.CrossHatchFilter;
import com.jme3.post.filters.DepthOfFieldFilter;
import com.jme3.post.filters.FogFilter;
import com.jme3.post.filters.LightScatteringFilter;
import com.jme3.post.filters.PosterizationFilter;
import com.jme3.post.ssao.SSAOFilter;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Sphere;
import com.jme3.scene.shape.Torus;
import com.jme3.shader.VarType;
import com.jme3.shadow.DirectionalLightShadowFilter;
import com.jme3.shadow.PointLightShadowFilter;
import com.jme3.shadow.SpotLightShadowFilter;
import com.jme3.water.WaterFilter;
import com.shaderblow.filter.basicssao.BasicSSAO;
import com.shaderblow.filter.frostedglass.FrostedGlassFilter;
import com.shaderblow.filter.oldfilm.OldFilmFilter;
import com.simsilica.lemur.geom.MBox;
import java.util.StringTokenizer;
import java.util.UUID;
import java.util.Vector;

/**
 *
 * @author David
 */
public class ObjectToElementConverter
{

    public static B3D_Element convertToElement(Object object)
    {
        B3D_Element element = null;
        if (object instanceof Filter)
        {
            element = convertFilter((Filter) object);
        } else if (object instanceof Light)
        {
            element = convertLight((Light) object);
        } else if (object instanceof Spatial)
        {
            element = convertSpatial((Spatial) object);
        } else if (object instanceof MotionEvent)
        {
            element = convertMotionEvent((MotionEvent) object);
        } else if (object instanceof Material)
        {
            element = convertMaterial((Material) object);
        }
        return element;
    }

    public static B3D_Filter convertFilter(Filter filter)
    {
        if (filter instanceof BasicSSAO)
        {
            return convertBasicSSAO((BasicSSAO) filter);
        } else if (filter instanceof BloomFilter)
        {
            return convertBloom((BloomFilter) filter);
        } else if (filter instanceof CartoonEdgeFilter)
        {
            return convertCartoon((CartoonEdgeFilter) filter);
        } else if (filter instanceof ColorScaleFilterWithGetters)
        {
            return convertColorScale((ColorScaleFilterWithGetters) filter);
        } else if (filter instanceof CrossHatchFilter)
        {
            return convertCrosshatch((CrossHatchFilter) filter);
        } else if (filter instanceof DepthOfFieldFilter)
        {
            return convertDOF((DepthOfFieldFilter) filter);
        } else if (filter instanceof LightScatteringFilter)
        {
            return convertLightScattering((LightScatteringFilter) filter);
        } else if (filter instanceof FogFilter)
        {
            return convertFog((FogFilter) filter);
        } else if (filter instanceof FrostedGlassFilter)
        {
            return convertFrostedGlass((FrostedGlassFilter) filter);
        } else if (filter instanceof OldFilmFilter)
        {
            return convertOldFilm((OldFilmFilter) filter);
        } else if (filter instanceof PosterizationFilter)
        {
            return convertPosterization((PosterizationFilter) filter);
        } else if (filter instanceof SSAOFilter)
        {
            return convertSSAO((SSAOFilter) filter);
        } else if (filter instanceof WaterFilter)
        {
            return convertWater((WaterFilter) filter);
        } else if (filter instanceof DirectionalLightShadowFilter)
        {
            return convertDirectionalLightShadow((DirectionalLightShadowFilterWithMapSizeGetter) filter);
        } else if (filter instanceof PointLightShadowFilter)
        {
            return convertPointLightShadow((PointLightShadowFilterWithMapSizeGetter) filter);
        } else if (filter instanceof SpotLightShadowFilter)
        {
            return convertSpotLightShadow((SpotLightShadowFilterWithMapSizeGetter) filter);
        }
        return null;
    }

    public static B3D_BasicSSAO convertBasicSSAO(BasicSSAO basicSSAO)
    {
        return new B3D_BasicSSAO(
                basicSSAO.getName(),
                basicSSAO.getBias(),
                basicSSAO.getDetailBias(),
                basicSSAO.getDetailIntensity(),
                basicSSAO.getDetailSampleRadius(),
                basicSSAO.getDetailScale(),
                basicSSAO.getFalloffRate(),
                basicSSAO.getFalloffStartDistance(),
                basicSSAO.getIntensity(),
                basicSSAO.getSampleRadius(),
                basicSSAO.getScale(),
                basicSSAO.isSmoothMore(),
                basicSSAO.isUseAo(),
                basicSSAO.isUseOnlyAo(),
                basicSSAO.getUseDetailPass(),
                basicSSAO.getUseDistanceFalloff(),
                basicSSAO.isUseSmoothing());
    }

    public static B3D_Bloom convertBloom(BloomFilter bloomFilter)
    {
        return new B3D_Bloom(
                bloomFilter.getName(),
                bloomFilter.getBloomIntensity(),
                bloomFilter.getBlurScale(),
                bloomFilter.getExposureCutOff(),
                bloomFilter.getExposurePower(),
                bloomFilter.getDownSamplingFactor());
    }

    public static B3D_DLShadow convertDirectionalLightShadow(DirectionalLightShadowFilterWithMapSizeGetter dlsf)
    {
        return new B3D_DLShadow(
                dlsf.getName(),
                dlsf.getShadowIntensity(),
                Wizard.getElementList().getElement(((DirectionalLightShadowFilter) dlsf).getLight()).getID(),
                dlsf.isFlushQueues(),
                dlsf.getEdgeFilteringMode(),
                dlsf.getEdgesThickness(),
                dlsf.getShadowMapSize());
    }

    public static B3D_SLShadow convertSpotLightShadow(SpotLightShadowFilterWithMapSizeGetter slsf)
    {
        return new B3D_SLShadow(
                slsf.getName(),
                slsf.getShadowIntensity(),
                Wizard.getElementList().getElement(((SpotLightShadowFilter) slsf).getLight()).getID(),
                slsf.isFlushQueues(),
                slsf.getEdgeFilteringMode(),
                slsf.getEdgesThickness(),
                slsf.getShadowMapSize());
    }

    public static B3D_PLShadow convertPointLightShadow(PointLightShadowFilterWithMapSizeGetter plsf)
    {
        return new B3D_PLShadow(
                plsf.getName(),
                plsf.getShadowIntensity(),
                Wizard.getElementList().getElement(((PointLightShadowFilter) plsf).getLight()).getID(),
                plsf.isFlushQueues(),
                plsf.getEdgeFilteringMode(),
                plsf.getEdgesThickness(),
                plsf.getShadowMapSize());
    }

    public static B3D_Cartoon convertCartoon(CartoonEdgeFilter cartoonEdgeFilter)
    {
        return new B3D_Cartoon(
                cartoonEdgeFilter.getName(),
                cartoonEdgeFilter.getEdgeColor(),
                cartoonEdgeFilter.getDepthSensitivity(),
                cartoonEdgeFilter.getDepthThreshold(),
                cartoonEdgeFilter.getEdgeIntensity(),
                cartoonEdgeFilter.getEdgeWidth(),
                cartoonEdgeFilter.getNormalSensitivity(),
                cartoonEdgeFilter.getNormalThreshold());
    }

    public static B3D_ColorScale convertColorScale(ColorScaleFilterWithGetters colorScaleFilter)
    {
        return new B3D_ColorScale(
                colorScaleFilter.getName(),
                colorScaleFilter.getColorDensity(),
                colorScaleFilter.isMultiply(),
                colorScaleFilter.isOverlay(),
                colorScaleFilter.getFilterColor());
    }

    public static B3D_Crosshatch convertCrosshatch(CrossHatchFilter crossHatchFilter)
    {
        return new B3D_Crosshatch(
                crossHatchFilter.getName(),
                crossHatchFilter.getLineColor(),
                crossHatchFilter.getPaperColor(),
                crossHatchFilter.getColorInfluenceLine(),
                crossHatchFilter.getColorInfluencePaper(),
                crossHatchFilter.getFillValue(),
                crossHatchFilter.getLineThickness(),
                crossHatchFilter.getLineDistance(),
                new Float[]
        {
            crossHatchFilter.getLuminance1(),
            crossHatchFilter.getLuminance2(),
            crossHatchFilter.getLuminance3(),
            crossHatchFilter.getLuminance4(),
            crossHatchFilter.getLuminance5()
        });
    }

    public static B3D_DepthOfField convertDOF(DepthOfFieldFilter depthOfFieldFilter)
    {
        return new B3D_DepthOfField(
                depthOfFieldFilter.getName(),
                depthOfFieldFilter.getBlurScale(),
                depthOfFieldFilter.getFocusDistance(),
                depthOfFieldFilter.getFocusRange());
    }

    public static B3D_LightScattering convertLightScattering(LightScatteringFilter lightScatteringFilter)
    {
        return new B3D_LightScattering(
                lightScatteringFilter.getName(),
                lightScatteringFilter.getBlurStart(),
                lightScatteringFilter.getBlurWidth(),
                lightScatteringFilter.getLightDensity(),
                lightScatteringFilter.getNbSamples(),
                lightScatteringFilter.getLightPosition());
    }

    public static B3D_Fog convertFog(FogFilter fogFilter)
    {
        return new B3D_Fog(fogFilter.getName(), fogFilter.getFogColor(), fogFilter.getFogDensity(), fogFilter.getFogDistance());
    }

    public static B3D_FrostedGlass convertFrostedGlass(FrostedGlassFilter frostedGlassFilter)
    {
        return new B3D_FrostedGlass(frostedGlassFilter.getName(), frostedGlassFilter.getRandomFactor(), frostedGlassFilter.getRandomScale());
    }

    public static B3D_OldFilm convertOldFilm(OldFilmFilter oldFilmFilter)
    {
        return new B3D_OldFilm(
                oldFilmFilter.getName(),
                oldFilmFilter.getFilterColor(),
                oldFilmFilter.getColorDensity(),
                oldFilmFilter.getNoiseDensity(),
                oldFilmFilter.getScratchDensity(),
                oldFilmFilter.getVignettingValue());
    }

    public static B3D_Posterization convertPosterization(PosterizationFilter posterizationFilter)
    {
        return new B3D_Posterization(
                posterizationFilter.getName(),
                posterizationFilter.getGamma(),
                posterizationFilter.getStrength(),
                posterizationFilter.getNumColors());
    }

    public static B3D_SSAO convertSSAO(SSAOFilter sSAOFilter)
    {
        return new B3D_SSAO(
                sSAOFilter.getName(),
                sSAOFilter.getSampleRadius(),
                sSAOFilter.getBias(),
                sSAOFilter.getIntensity(),
                sSAOFilter.getScale());
    }

    public static B3D_Water convertWater(WaterFilter waterFilter)
    {
        return new B3D_Water(
                waterFilter.getName(),
                waterFilter.getCausticsIntensity(),
                waterFilter.getFoamHardness(),
                waterFilter.getFoamIntensity(),
                waterFilter.getMaxAmplitude(),
                waterFilter.getNormalScale(),
                waterFilter.getReflectionDisplace(),
                waterFilter.getReflectionMapSize(),
                waterFilter.getRefractionStrength(),
                waterFilter.getShininess(),
                waterFilter.getShoreHardness(),
                waterFilter.getSpeed(),
                waterFilter.getSunScale(),
                waterFilter.getUnderWaterFogDistance(),
                waterFilter.getWaterHeight(),
                waterFilter.getWaterTransparency(),
                waterFilter.getWaveScale(),
                waterFilter.getWindDirection(),
                waterFilter.getColorExtinction(),
                waterFilter.getFoamExistence(),
                waterFilter.getLightDirection(),
                waterFilter.getWaterColor(),
                waterFilter.getDeepWaterColor(),
                waterFilter.getLightColor(),
                waterFilter.isUseCaustics(),
                waterFilter.isUseFoam(),
                waterFilter.isUseHQShoreline(),
                waterFilter.isUseRefraction(),
                waterFilter.isUseRipples(),
                waterFilter.isUseSpecular());
    }

    public static B3D_Light convertLight(Light light)
    {
        if (light instanceof DirectionalLight)
        {
            return convertDirectionalLight((DirectionalLight) light);
        } else if (light instanceof AmbientLight)
        {
            return convertAmbientLight((AmbientLight) light);
        } else if (light instanceof PointLight)
        {
            return convertPointLight((PointLight) light);
        } else if (light instanceof SpotLight)
        {
            return convertSpotLight((SpotLight) light);
        }
        return null;
    }

    public static B3D_DirectionalLight convertDirectionalLight(DirectionalLight directionalLight)
    {
        return new B3D_DirectionalLight(directionalLight.getName(), directionalLight.getColor(), directionalLight.getDirection());
    }

    public static B3D_AmbientLight convertAmbientLight(AmbientLight ambientLight)
    {
        return new B3D_AmbientLight(ambientLight.getName(), ambientLight.getColor());
    }

    public static B3D_PointLight convertPointLight(PointLight pointLight)
    {
        return new B3D_PointLight(pointLight.getName(), pointLight.getColor(), pointLight.getPosition(), pointLight.getRadius());
    }

    public static B3D_SpotLight convertSpotLight(SpotLight spotLight)
    {
        return new B3D_SpotLight(
                spotLight.getName(),
                spotLight.getColor(),
                spotLight.getPosition(),
                spotLight.getDirection(),
                spotLight.getSpotInnerAngle(),
                spotLight.getSpotOuterAngle(),
                spotLight.getSpotRange());
    }

    public static B3D_Spatial convertSpatial(Spatial spatial)
    {
        B3D_Spatial b3D_Spatial = null;
        if (spatial instanceof Node)
        {
            b3D_Spatial = convertNode((Node) spatial);
        } else if (spatial instanceof Geometry)
        {
            Geometry geometry = (Geometry) spatial;
            if (spatial.getUserData("modelName") != null)
            {
                b3D_Spatial = convertModel(geometry);
            } else if (geometry.getMesh() instanceof MBox)
            {
                b3D_Spatial = convertBox(geometry);
            } else if (geometry.getMesh() instanceof Cylinder)
            {
                b3D_Spatial = convertCylinder(geometry);
            } else if (spatial.getUserData("north") != null)
            {
                if (spatial.getUserData("south") != null)
                {
                    b3D_Spatial = convertMultipleTexturesSkyBox(geometry);
                } else
                {
                    b3D_Spatial = convertSingleTextureSkyBox(geometry);
                }
            } else if (geometry.getMesh() instanceof Sphere)
            {
                b3D_Spatial = convertSphere(geometry);
            } else if (geometry.getMesh() instanceof Torus)
            {
                b3D_Spatial = convertTorus(geometry);
            } else if (geometry instanceof ParticleEmitter)
            {
                b3D_Spatial = convertParticleEmitter((ParticleEmitter) geometry);
            }
        }
        if (spatial.getUserData("parentID") != null)
        {
            //System.out.println("Parent ID: " + spatial.getUserData("parentID").toString());
            b3D_Spatial.setParentID(UUID.fromString(spatial.getUserData("parentID").toString()));
        }
        b3D_Spatial.setScale(spatial.getLocalScale());
        b3D_Spatial.setAngles((Vector3f) spatial.getUserData("angles"));
        b3D_Spatial.setTranslation(spatial.getLocalTranslation());
        b3D_Spatial.setRotation(spatial.getLocalRotation());
        switch (spatial.getShadowMode())
        {
            case Cast:
                b3D_Spatial.setShadowMode("Cast");
                break;
            case Receive:
                b3D_Spatial.setShadowMode("Recieve");
                break;
            case CastAndReceive:
                b3D_Spatial.setShadowMode("Cast&Recieve");
                break;
            case Inherit:
                b3D_Spatial.setShadowMode("Inherit");
                break;
            default:
                b3D_Spatial.setShadowMode("Off");
                break;
        }
        if (spatial.getControl(RigidBodyControl.class) != null)
        {
            b3D_Spatial.setPhysics(convertPhysics(spatial, b3D_Spatial));
        }
        return b3D_Spatial;
    }

    public static B3D_Node convertNode(Node node)
    {
        Vector<B3D_Spatial> b3D_Spatials = new Vector<B3D_Spatial>();
        for (Spatial spatial : node.getChildren())
        {
            b3D_Spatials.add(convertSpatial(spatial));
        }
        return new B3D_Node(node.getName(), b3D_Spatials);
    }

    public static B3D_Box convertBox(Geometry geometry)
    {
        B3D_Material b3D_Material = convertMaterial(geometry.getMaterial());
        return new B3D_Box(
                geometry.getName(),
                b3D_Material,
                (Integer) geometry.getUserData("xSlices"),
                (Integer) geometry.getUserData("ySlices"),
                (Integer) geometry.getUserData("zSlices"));
    }

    public static B3D_Model convertModel(Geometry geometry)
    {
        return new B3D_Model(geometry.getName(), geometry.getUserData("modelName").toString(), convertMaterial(geometry.getMaterial()));
    }

    public static B3D_Cylinder convertCylinder(Geometry geometry)
    {
        return new B3D_Cylinder(
                geometry.getName(),
                convertMaterial(geometry.getMaterial()),
                ((Cylinder) geometry.getMesh()).getAxisSamples(),
                ((Cylinder) geometry.getMesh()).getRadialSamples(),
                ((Cylinder) geometry.getMesh()).getRadius(),
                ((Cylinder) geometry.getMesh()).getRadius2(),
                ((Cylinder) geometry.getMesh()).getHeight(),
                ((Cylinder) geometry.getMesh()).isClosed(),
                ((Cylinder) geometry.getMesh()).isInverted());
    }

    public static B3D_ParticleEffect convertParticleEmitter(ParticleEmitter particleEmitter)
    {
        B3D_ParticleEffect.Type type;
        if (particleEmitter.getMeshType().equals(ParticleMesh.Type.Point))
        {
            type = B3D_ParticleEffect.Type.Point;
        } else
        {
            type = B3D_ParticleEffect.Type.Triangle;
        }
        return new B3D_ParticleEffect(
                particleEmitter.getName(),
                particleEmitter.getMaterial().getParam("Texture").toString(),
                particleEmitter.getGravity(),
                particleEmitter.getParticleInfluencer().getInitialVelocity(),
                particleEmitter.getStartSize(),
                particleEmitter.getEndSize(),
                particleEmitter.getStartColor(),
                particleEmitter.getEndColor(),
                particleEmitter.getParticleInfluencer().getVelocityVariation(),
                particleEmitter.getHighLife(),
                particleEmitter.getLowLife(),
                particleEmitter.getRotateSpeed(),
                particleEmitter.getParticlesPerSec(),
                particleEmitter.getImagesX(),
                particleEmitter.getImagesY(),
                particleEmitter.getMaxNumParticles(),
                particleEmitter.getMaterial().getAdditionalRenderState().isDepthWrite(),
                type);
    }

    public static B3D_MultipleTextureSkyBox convertMultipleTexturesSkyBox(Spatial spatial)
    {
        return new B3D_MultipleTextureSkyBox(
                spatial.getName(),
                spatial.getUserData("north").toString(),
                spatial.getUserData("south").toString(),
                spatial.getUserData("west").toString(),
                spatial.getUserData("east").toString(),
                spatial.getUserData("top").toString(),
                spatial.getUserData("bottom").toString());
    }

    public static B3D_SingleTextureSkyBox convertSingleTextureSkyBox(Spatial spatial)
    {
        return new B3D_SingleTextureSkyBox(spatial.getName(), spatial.getUserData("north").toString());
    }

    public static B3D_Sphere convertSphere(Geometry geometry)
    {
        return new B3D_Sphere(
                geometry.getName(),
                convertMaterial(geometry.getMaterial()),
                ((Sphere) geometry.getMesh()).getRadialSamples(),
                ((Sphere) geometry.getMesh()).getZSamples(),
                ((Sphere) geometry.getMesh()).getRadius());
    }

    public static B3D_Torus convertTorus(Geometry geometry)
    {
        return new B3D_Torus(
                geometry.getName(),
                convertMaterial(geometry.getMaterial()),
                ((Torus) geometry.getMesh()).getCircleSamples(),
                ((Torus) geometry.getMesh()).getRadialSamples(),
                ((Torus) geometry.getMesh()).getInnerRadius(),
                ((Torus) geometry.getMesh()).getOuterRadius());
    }

    public static B3D_Material convertMaterial(Material material)
    {
        B3D_MaterialPropertyList b3D_MaterialPropertyList = new B3D_MaterialPropertyList();
        for (MatParam matParam : material.getParams())
        {
            if (!b3D_MaterialPropertyList.has(matParam.getName()))
            {
                VarType varType = matParam.getVarType();
                if (varType.equals(VarType.Boolean))
                {
                    //System.out.println("PropertyList - Boolean: " + matParam.getName() + " -> " + matParam.getValueAsString());
                    b3D_MaterialPropertyList.add(matParam.getName(), "boolean", matParam.getValueAsString());
                } else if (varType.equals(VarType.Vector4))
                {
                    //System.out.println("PropertyList - Color: " + matParam.getName() + " -> " + matParam.getValueAsString());
                    b3D_MaterialPropertyList.add(matParam.getName(), "color", matParam.getValueAsString());
                } else if (varType.equals(VarType.Texture2D) || varType.equals(VarType.Texture3D))
                {
                    //System.out.println("PropertyList - Texture: " + matParam.getName() + " -> " + matParam.getValueAsString());
                    String newValue = matParam.getValueAsString();
                    if (newValue.startsWith("Flip "))
                    {
                        newValue = newValue.substring(5);
                    }
                    /*StringTokenizer tokenizer = new StringTokenizer(newValue, " ");
                     while (tokenizer.hasMoreTokens())
                     {
                     System.out.println("Correctiong " + newValue);
                     String nextToken = tokenizer.nextToken();
                     if (!nextToken.contains("."))
                     {
                     newValue = newValue.replaceAll(nextToken, "");
                     }
                     newValue = newValue.replaceAll(" ", "");
                     }*/
                    //System.out.println("PropertyList - Texture (second attempt): " + matParam.getName() + " -> " + newValue);
                    {
                        b3D_MaterialPropertyList.add(matParam.getName(), "texture", newValue);
                    }
                } else if (varType.equals(VarType.Float))
                {
                    //System.out.println("PropertyList - Float: " + matParam.getName() + " -> " + matParam.getValueAsString());
                    b3D_MaterialPropertyList.add(matParam.getName(), "float", matParam.getValueAsString());
                } else if (varType.equals(VarType.Int))
                {
                    //System.out.println("PropertyList - Int: " + matParam.getName() + " -> " + matParam.getValueAsString());
                    b3D_MaterialPropertyList.add(matParam.getName(), "int", matParam.getValueAsString());
                } else if (varType.equals(VarType.Vector2))
                {
                    //System.out.println("PropertyList - Vector2: " + matParam.getName() + " -> " + matParam.getValueAsString());
                    b3D_MaterialPropertyList.add(matParam.getName(), "vector2", matParam.getValueAsString());
                } else if (varType.equals(VarType.Vector2))
                {
                    //System.out.println("PropertyList - Vector2: " + matParam.getName() + " -> " + matParam.getValueAsString());
                    b3D_MaterialPropertyList.add(matParam.getName(), "vector2", matParam.getValueAsString());
                }
            }
        }
        return new B3D_Material(material.getMaterialDef().getAssetName(), b3D_MaterialPropertyList);
    }

    public static B3D_Physics convertPhysics(Spatial spatial, B3D_Spatial b3D_Spatial)
    {
        B3D_CShape b3D_Shape = convertCollisionShape(spatial.getControl(RigidBodyControl.class).getCollisionShape(), spatial, b3D_Spatial);
        return new B3D_Physics(
                spatial.getControl(RigidBodyControl.class).getMass(),
                spatial.getControl(RigidBodyControl.class).getRestitution(),
                b3D_Shape,
                spatial.getControl(RigidBodyControl.class).isKinematic());
    }

    public static B3D_CShape convertCollisionShape(CollisionShape collisionShape, Spatial spatial, B3D_Spatial b3D_Spatial)
    {
        if (collisionShape instanceof BoxCollisionShape)
        {
            return new B3D_BoxShape(((BoxCollisionShape) collisionShape).getHalfExtents());
        } else if (collisionShape instanceof CapsuleCollisionShape)
        {
            return new B3D_CapsuleShape(((CapsuleCollisionShape) collisionShape).getRadius(), ((CapsuleCollisionShape) collisionShape).getHeight());
        } else if (collisionShape instanceof ConeCollisionShape)
        {
            return new B3D_ConeShape(((ConeCollisionShape) collisionShape).getRadius(), ((ConeCollisionShape) collisionShape).getHeight());
        } else if (collisionShape instanceof CylinderCollisionShape)
        {
            return new B3D_CylinderShape(((CylinderCollisionShape) collisionShape).getHalfExtents());
        } else if (spatial.getUserData("cShape").equals("static"))
        {
            return new B3D_StaticMeshShape(b3D_Spatial.getID());
        } else if (spatial.getUserData("cShape").equals("dynamic"))
        {
            return new B3D_DynamicMeshShape(b3D_Spatial.getID());
        }
        return null;
    }

    public static B3D_Element convertMotionEvent(MotionEvent motionEvent)
    {
        B3D_MotionEvent b3D_MotionEvent;
        B3D_MotionPath b3D_MotionPath;
        UUID movingObject = null;
        Object lookAtObject = ((B3D_MotionEvent) Wizard.getElementList().getElement(motionEvent)).getMotionPath().getLookAtObject();
        ColorRGBA color = ColorRGBA.Green;
        if (!(motionEvent.getSpatial() instanceof CameraNode))
        {
            movingObject = Wizard.getElementList().getElement(motionEvent.getSpatial()).getID();
        }
        if (lookAtObject != null)
        {
            if (lookAtObject.equals("Camera"))
            {
                color = ColorRGBA.Gray;
            }
        }
        b3D_MotionPath = new B3D_MotionPath(
                lookAtObject,
                motionEvent.getPath().getCurveTension(),
                motionEvent.getSpeed(),
                color,
                motionEvent.getPath().isCycle(),
                motionEvent.getRotation(),
                motionEvent.getDirectionType(),
                motionEvent.getLoopMode());
        for (int i = 0; i < motionEvent.getPath().getNbWayPoints(); i++)
        {
            b3D_MotionPath.getWayPoints().add(motionEvent.getPath().getWayPoint(i));
        }
        b3D_MotionEvent = new B3D_MotionEvent(
                Wizard.getElementList().getElement(motionEvent).getName(),
                movingObject,
                b3D_MotionPath);
        return b3D_MotionEvent;
    }
}
